#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    /*创建文件*/
    const char *filename = "./record.txt";
    /*打开文件*/
    FILE *fp = fopen(filename, "a+");
    if (fp == NULL)
    {
        perror("fopen error.");
        exit(-1);
    }
    // const char *data = "hello world.\n";
    // /*写入数据到文件中 */
    // int writeBytes = fwrite(data, 1, strlen(data), fp);
#if 0
    char name[20] = {};
    printf("请输入学生的姓名:");
    scanf("%s", name);

    char passwd[20] = {};
    printf("请输入学生的密码:");
    scanf("%s", passwd);


    int writeBytes = fwrite(name, 1, strlen(name), fp);
    fwrite("\n",1,1,fp);
    writeBytes = fwrite(passwd, 1, strlen(passwd), fp);
    fwrite("\n",1,1,fp);
#endif

#if 0
    char bufline[5] = {0};
    fread(bufline,1,sizeof(bufline)-1,fp);
    printf("%s",bufline);
#endif
#if 0
    /*读取一行数据*/
    char *lineData = NULL;
    size_t size = 0;

    while (1)
    {
        __ssize_t readBytes = getline(&lineData, &size, fp);
        if (readBytes <= 0)
        {
            break;
        }
        printf("%s", lineData);
    }
#endif

    char *name = "zhangsan!";
    fwrite(name, 1, strlen(name), fp);

    /* 重定向光标的位置。*/
    fseek(fp, 2, SEEK_SET);

    name = "lisi";
    fwrite(name, 1, strlen(name), fp);

    /*关闭文件*/
    fclose(fp);
    return 0;
}